package com.ruoyi.lab.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.google.api.client.util.Joiner;
import com.google.common.collect.ImmutableMap;
import com.mysql.cj.x.protobuf.Mysqlx;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.lab.domain.JacTestOutline;
import com.ruoyi.lab.domain.JacTestProjectLog;
import com.ruoyi.lab.domain.JacTestResult;
import com.ruoyi.lab.mapper.JacTestOutlineMapper;
import com.ruoyi.lab.mapper.JacTestProjectLogMapper;
import com.ruoyi.lab.mapper.JacTestResultMapper;
import com.ruoyi.lab.service.IJacTestProjectLogService;
import com.ruoyi.lab.utils.MyTool;
import com.ruoyi.lab.utils.TimeTool;
import com.ruoyi.lab.utils.StatusTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lab.mapper.JacTestProjectMapper;
import com.ruoyi.lab.domain.JacTestProject;
import com.ruoyi.lab.service.IJacTestProjectService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.servlet.http.HttpServletRequest;
import static com.ruoyi.common.datasource.DynamicDataSourceContextHolder.log;
import com.ruoyi.common.core.controller.BaseController;

/**
 * 实验计划业务处理层
 * projectService业务层处理
 * author sxx
 * date 2021-05-19
 */

@Service
public class JacTestProjectServiceImpl extends BaseController implements IJacTestProjectService {
    @Autowired
    private JacTestProjectMapper jacTestProjectMapper;
    @Autowired
    private JacTestOutlineMapper jacTestOutlineMapper;
    @Autowired
    private JacTestProjectLogMapper jacTestProjectLogMapper;
    @Autowired
    private JacTestResultMapper jacTestResultMapper;
    private static final String OK = "1";

    /**
     * 根据id查询project
     */

    @Override
    public JacTestProject selectJacTestProjectById(Long id) {
        return jacTestProjectMapper.selectJacTestProjectById(id);
    }

    /**
     * project详情页
     * 查询project
     * 传递参数 jacTestOutline projectcode
     * 返回结果 result
     */

    @Override
    public List<JacTestProject> selectJacTestProjectList(JacTestOutline jacTestOutline) {
        HttpServletRequest request = ServletUtils.getRequest();
        String usertype = jacTestOutlineMapper.selectUserTypeById(request.getHeader(Constants.CURRENT_ID));
        jacTestOutline.setUserid(request.getHeader(Constants.CURRENT_ID));
        if (usertype.equals("01")) {
            if ("第三方试验室".equals(jacTestOutlineMapper.selectSysUserRemarkByUserId(jacTestOutline.getUserid())))
                usertype = "02";
        }
        if ("00".equals(usertype)) {
            startPage();
            return jacTestProjectMapper.selectJacTestProjectListN(jacTestOutline.getProjectcode());
        } else if ("01".equals(usertype)){
            startPage();
            return jacTestProjectMapper.selectJacTestProjectListNewDouble(jacTestOutline.getProjectcode());
        } else {
            startPage();
            return jacTestProjectMapper.selectJacTestProjectListTest(jacTestOutline);
        }
    }

    /**
     * 计划保存按钮
     * 批量新增project
     * 传递参数 jacTestProject
     * 返回结果 result
     */

    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    @Override
    public String insertJacTestProject(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        Long itemcode=jacTestProjects.get(0).getItemcode();
        List<String> list = new ArrayList<String>();
        for(JacTestProject jacTestProject1 : jacTestProjects) {
            if(jacTestProject1.getExendtime().compareTo(jacTestProject1.getExbegintime()) < 0) {
                  result="试验名称为 "+jacTestProject1.getExperimentname()+"的试验，试验开始时间"+jacTestProject1.getExbegintime()+"，晚于试验结束时间"+jacTestProject1.getExendtime()+"请检查后重新保存";
                list.add(result);
                log.error(result);
            }
        }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
            return result;
        }
        for(JacTestProject jacTestProject1 : jacTestProjects) {
            if(!(jacTestProjectMapper.experimentsiteIsResearch(jacTestProject1.getExperimentsite()))) {
                try{
                    jacTestProjectMapper.insertJacTestProject(jacTestProject1);
                } catch (Exception e) {
                    result = "试验名称为 " + jacTestProject1.getExperimentname() + "的试验重复，插入失败。请联系管理员!";
                    list.add(result);
                    log.error(result, e);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }else{
                result = "试验院：" + jacTestProject1.getExperimentsite() + "不可成为试验地点。";
                list.add(result);
                log.error(result);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
        }
        JacTestOutline jto = jacTestOutlineMapper.selectJacTestOutlineByIdNew(itemcode);
        if(null==jto.getLastfinishtime()&&null!=jto.getFinishtime()){
            jto.setLastfinishtime(TimeTool.DateToStringDay(jto.getFinishtime()));
            jacTestOutlineMapper.updateJacTestOutline(jto);
        }
        return getResultAndCheckOutlineStatus(result,itemcode);
    }


    /**
     * 计划更新按钮
     * 更新project
     * 传递参数 jacTestProject （ids，itemcode)
     * 返回参数 result
     */


    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    @Override
    public String updateJacTestProject(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        List<String> list = new ArrayList<String>();
        List<JacTestProject> jp = new ArrayList<>();
        Long itemcode=jacTestProjects.get(0).getItemcode();
        for (JacTestProject jj : jacTestProjects) {
            jp.add(jacTestProjectMapper.selectJacTestProjectById(jj.getId()));
        }
        for (JacTestProject jj : jp) {
           if(!"beforeaudit".equals(jj.getPstage())){
               result="计划"+jj.getExperimentname()+"已提交不可更新！";
               list.add(result);
           }
        }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
            return result;
        }
        for (JacTestProject jacTestProject1 : jacTestProjects) {
            if (!(jacTestProjectMapper.experimentsiteIsResearch(jacTestProject1.getExperimentsite()))) {
                try {
                    jacTestProjectMapper.updateJacTestProject(jacTestProject1);
                } catch (Exception e) {
                    result = "更新试验号" + jacTestProject1.getId() + "失败。请联系管理员!";
                    list.add(result);
                    log.error(result, e);
                }
            } else {
                result = "试验院" + jacTestProject1.getExperimentsite() + "不可成为试验地点!";
                list.add(result);
                log.error(result);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
        }
        return getResultAndCheckOutlineStatus(result,itemcode);
    }



    /**
     * 计划删除按钮
     * 删除project对象
     * @param ids 需要删除的数据ID
     * @return 结果
     */                                       


    @Override
    public String deleteJacTestProjectByIds (String ids) {
        String result = OK;
        if(null==ids||"".equals(ids)){
            result="计划未保存，无需删除！";
            return result;
        }
        List<String> list = new ArrayList<String>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        Long[] myArray = Convert.toLongArray(ids);
        Long itemcode=0l;

        for (Long s : myArray) {
            JacTestProject jacTestProject = jacTestProjectMapper.selectJacTestProjectById(s);
             itemcode=jacTestProject.getItemcode();
            if(!("beforeaudit".equals(jacTestProject.getPstage()))){
                result=jacTestProject.getExperimentname()+"非创建状态，不能删除！";
                list.add(result);
            }
        }
        if (list.size() != 0) {
            return Joiner.on(',').join(list);
        }
        String[] flag=  Convert.toStrArray(ids);
        jacTestProjectMapper.deleteJacTestProjectByIds(flag);
        JacTestOutline jto = jacTestOutlineMapper.selectJacTestOutlineByIdNew(itemcode);
        List<JacTestProject> jtp = jacTestProjectMapper.selectJacTestProjectByItemcode(itemcode);
        long count =0l;
        if(null!=jto.getItemresult()){
            for (JacTestProject jd:jtp) {
                if(!"complete".equals(jd.getExperimentstatus()))
                    break;
                else{
                    count++;
                    continue;
                }
            }
        }
        if(jtp.size()==count&&null!=jto.getItemresult()){
            jto.setStatus("finish");
            try {
                jto.setFinishtime(TimeTool.StringToDate(jto.getLastfinishtime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            jto.setLastfinishtime(null);
            jacTestOutlineMapper.updateJacTestOutline(jto);
            return result;
        }
        return getResultAndCheckOutlineStatus(result,itemcode);
    }

    /**
     * 通过id删除project信息
     */

    @Override
    public int deleteJacTestProjectById(Long id) {
        return jacTestProjectMapper.deleteJacTestProjectById(id);
    }

    /**
     * 计划提交按钮
     * 供应商录入project、提交给试验院审核
     * 传递参数 jacTestProject ids
     * 返回参数 result
     */

    @Override
    public String submitJacTestProjectById(String ids) {
        String result = OK;
        List<String> list=new ArrayList<>();
        if(null==ids||"".equals(ids)){
            result="请先保存试验计划再提交！";
            return result;
        }
        Long[] myarray = Convert.toLongArray(ids);
        Long itemcode=0l;
        for (Long s : myarray) {
            JacTestProject jtp = jacTestProjectMapper.selectJacTestProjectById(s);
            itemcode=jtp.getItemcode();
          if("beforeaudit".equals(jtp.getPstage())) {
              jtp.setPstage("commit");
              jacTestProjectMapper.updateJacTestProject(jtp);
          }else{
              result=jtp.getExperimentname()+"计划正在进行或已提交,不允许进行提交操作！";
              list.add(result);
          }
        }
        if(list.size()>0) return Joiner.on(',').join(list);
        return getResultAndCheckOutlineStatus(result,itemcode);
    }

    /**
     * 审核计划通过按钮
     * 试验院审核计划通过project
     * 传递参数 jacTestProject （itemcode 、id）
     * 返回参数 result
     */

    @Override
    public String examineJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        Long itemcode = jacTestProjects.get(0).getItemcode();
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if (!("create".equals(jj.getExperimentstatus()))) {
                finalString.add("计划" + jj.getExperimentname() + "正在进行或已完成，不允许通过审核，请重新进行审核操作");
            }
        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
        for (JacTestProject jt : jacTestProjectList) {
            jt.setExperimentstatus("ongoing");
            jt.setPstage("beforeauditnew");
            jacTestProjectMapper.updateJacTestProject(jt);
        }
        return getResultAndCheckOutlineStatus(result,itemcode);
    }

    /**
     * 审核计划失败按钮
     * 试验院审核计划失败project
     * 传递参数 jacTestProject （itemcode 、id）
     * 返回参数 result
     */

    @Override
    public String failJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        Long itemcode = jacTestProjects.get(0).getItemcode();
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if (!("create".equals(jj.getExperimentstatus()))) {
                finalString.add("计划" + jj.getExperimentname() + "正在进行或已完成，不允许退回审核，请重新进行审核操作");
            }
        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
        for (JacTestProject jt : jacTestProjectList){
            jt.setExperimentstatus("create");
            jt.setPstage("beforeaudit");
            jacTestProjectMapper.updateJacTestProject(jt);
            jacTestProjectLogMapper.insertJacTestProjectLog(MyTool.ConvertJacTestProjectTOJacTestLogProject(jt));
        }

        return getResultAndCheckOutlineStatus(result,itemcode);
    }

    /**
     * 供应商录入实验结果后提交计划project按钮
     * 传递参数 试验判断位 pstage、 试验项目代码 itemcode以及 试验id和 experimentresult、experimentname
     * 返回参数 result
     */

    @Override
    public String submitSelectedJacTestProjectById(List<JacTestProject> jacTestProjectList) {
        JacTestProject jacTestProject = jacTestProjectList.get(0);
        Long itemcode=jacTestProject.getItemcode();
        String result = OK;
        List<JacTestResult> list = new ArrayList<>();
        for (JacTestProject jtp:jacTestProjectList) {
            if(!("beforeauditnew".equals(jtp.getPstage())))
            {
                result="所选提交计划中，存在已提交或已完成的计划，或未到结果提交节点不允许提交！"+"， 请确认后重新进行提交操作！";
                return result;
            }
        }
        for (JacTestProject jtp : jacTestProjectList) {
            List<JacTestResult> jtr = jacTestResultMapper.selectJacTestResultListNew(jtp.getId());
            if (jtr.size() == 0) {
                result = "提交失败，计划任务"+jtp.getExperimentname()+"未完成，请录入试验结果及试验进展描述！";
                return result;
            } else {
                list.addAll(jtr);
            }
        }
        for (JacTestResult jt : list) {
            if (null == jt.getExperimentresult() || "".equals(jt.getExperimentresult())) {
                result = "提交失败，尚有试验结果未填写！";
                return result;
            }
        }
        for (JacTestProject jtp : jacTestProjectList) {
            if ("new".equals(jtp.getExperimentresult())) {
                result = "提交失败，计划："+jtp.getExperimentname()+"供应商未录入评判结果！";
                return result;
            }
        }
        for (JacTestProject jtp : jacTestProjectList) {
          jtp.setPstage("secondcommit");
          jtp.setExperimentstatus("ongoing");
          jacTestProjectMapper.updateJacTestProject(jtp);
        }
        return getResultAndCheckOutlineStatus(result,itemcode);
    }


    /**
     * 试验院审核结果通过按钮
     * 审核结果通过project
     * 传入参数： jacTestProject （itemcode 、id）
     * 返回结果： result
     */


    @Override
    public String examineResultJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        Long itemcode = jacTestProjects.get(0).getItemcode();
        JacTestOutline jto = jacTestOutlineMapper.selectJacTestOutlineByIdNew(itemcode);
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if ("complete".equals(jj.getExperimentstatus())) {
                finalString.add("计划" + jj.getExperimentname() + "已审核成功，请勿重复审核！");
            }
            if("commit".equals(jj.getPstage())){
                finalString.add("计划"+jj.getExperimentname()+"正在提交，不允许审核结果！");
            }
        }
        if(null==jto.getItemresult()){
            finalString.add("尚未提交总体试验结果！");
        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
        for (JacTestProject jt : jacTestProjectList) {
            jt.setExperimentstatus("complete");
            jt.setPstage("afterauditnew");
            jacTestProjectMapper.updateJacTestProject(jt);
        }
        jto.setStatus("finish");
        jto.setFinishtime(new Date());
        jto.setLastfinishtime(null);
        jacTestOutlineMapper.updateJacTestOutline(jto);
       return  result;
    }


    /**
     * 试验院结果审核失败按钮
     * 审核结果失败project
     * 传入参数： jacTestProject（itemcode、id）
     * 返回结果： result
     */


    @Override
    public String failResultJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject) {
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        Long itemcode = jacTestProjects.get(0).getItemcode();
        JacTestOutline jto = jacTestOutlineMapper.selectJacTestOutlineByIdNew(itemcode);
        if(null==jto.getItemresult()){
            finalString.add("尚有总体试验结果未提交！");
        }
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if ("complete".equals(jj.getExperimentstatus())) {
                finalString.add("计划" + jj.getExperimentname() + "已审核通过，不允许审核失败！");
            }
            if("commit".equals(jj.getPstage())){
                finalString.add("计划"+jj.getExperimentname()+"正在提交，不允许审核结果！");
            }
        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
       for (JacTestProject jt : jacTestProjectList) {
             jt.setExperimentstatus("notstandard");
             jt.setPstage("beforeauditnew");
             jacTestProjectMapper.updateJacTestProject(jt);
             jacTestProjectLogMapper.insertJacTestProjectLog(MyTool.ConvertJacTestProjectTOJacTestLogProject(jt));
        }
       jto.setStatus("unqualified");
       jacTestOutlineMapper.setFinishTimeNUll(itemcode);
       jacTestOutlineMapper.updateJacTestOutline(jto);
        return  result;
    }

    /**
     * 供应商评判成功按钮
     * 供应商评判结果成功project
     * 传入参数： jacTestProject（itemcode、id、experimentname）
     * 返回结果： result
     */


    @Override
    public String supplierExamineJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject){
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        for (JacTestProject j:jacTestProjects) {
            if(null==j.getId()||"".equals(j.getId())){
                result="有未保存的试验计划，请检查后重新评判！";
                return result;
            }
        }
        Long itemcode = jacTestProjects.get(0).getItemcode();
        List<JacTestResult> list = new ArrayList<>();
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if(!"beforeauditnew".equals(jj.getPstage())){
                finalString.add("计划"+jj.getExperimentname()+"非供应商评判节点，不允许评判结果！");
            }
        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
        for (JacTestProject jt : jacTestProjectList) {
            jt.setExperimentresult("acceptable");
            jacTestProjectMapper.updateJacTestProject(jt);
        }
        return getResultAndCheckOutlineStatus(result, itemcode);
    }

    /**
     * 供应商评判失败按钮
     * 供应商评判结果失败project
     * 传入参数： jacTestProject（itemcode、id、experimentname）
     * 返回结果： result
     */


    @Override
    public String supplierFailJacTestProjectById(Map<String, List<JacTestProject>> jacTestProject){
        String result = OK;
        List<String> finalString = new ArrayList<>();
        List<JacTestProject> jacTestProjectList = new ArrayList<>();
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        for (JacTestProject j:jacTestProjects) {
            if(null==j.getId()||"".equals(j.getId())){
                result="有未保存的试验计划，请检查后重新评判！";
                return result;
            }
        }
        Long itemcode = jacTestProjects.get(0).getItemcode();
        List<JacTestResult> list = new ArrayList<>();
        for (JacTestProject k : jacTestProjects) {
            jacTestProjectList.add(jacTestProjectMapper.selectJacTestProjectById(k.getId()));
        }
        for (JacTestProject jj : jacTestProjectList) {
            if(!"beforeauditnew".equals(jj.getPstage())){
                finalString.add("计划"+jj.getExperimentname()+"非供应商评判节点，不允许评判结果！");
            }

        }
        if (finalString.size() != 0)
            return Joiner.on(',').join(finalString);
        for (JacTestProject jt : jacTestProjectList) {
            jt.setExperimentresult("unqualified");
            jacTestProjectMapper.updateJacTestProject(jt);
        }
        return getResultAndCheckOutlineStatus(result, itemcode);
    }




    /**
     * 此方法用来更改project的试验阶段字段
     * 传递参数： experimentsite、ptage、itemcode、experimentname
     * 返回结果： result
     */



    public String updateJacTestProjectExperimentStage(Map<String, List<JacTestProject>> jacTestProject){

        String result = OK;
        List<JacTestProject> jacTestProjects = jacTestProject.get("datas");
        List<String> list = new ArrayList<String>();
        Long itemcode=jacTestProjects.get(0).getItemcode();
        JacTestOutline jacTestOutline=jacTestOutlineMapper.selectJacTestOutlineById(itemcode);
        for (JacTestProject jj : jacTestProjects) {
            if("commit".equals(jj.getPstage())||"secondcommit".equals(jj.getPstage())){
                result="计划"+jj.getExperimentname()+"正在审核不可更新！";
                list.add(result);
            }
            if("finish".equals(jacTestOutline.getStatus())){
                result="计划"+jacTestOutline.getItemcode()+"已完成不可更新！";
                list.add(result);
            }
        }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
            return result;
        }
        for (JacTestProject jacTestProject1 : jacTestProjects) {
            if (!(jacTestProjectMapper.experimentsiteIsResearch(jacTestProject1.getExperimentsite()))) {
                try {
                    jacTestProjectMapper.updateJacTestProject(jacTestProject1);
                } catch (Exception e) {
                    result = "更新试验号" + jacTestProject1.getId() + "失败。请联系管理员!";
                    list.add(result);
                    log.error(result, e);
                }
            } else {
                result = "试验院" + jacTestProject1.getExperimentsite() + "不可成为试验地点!";
                list.add(result);
                log.error(result);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
              return result;
    }


    /**
     * 试验院重置实验计划状态按钮
     * 此方法用来重置project的为初始创建
     * 传递参数： ids
     * 返回结果： result
     */


    public String backJacTestProjectToCreate(String ids){
        String result = OK;
        Long[] myids=Convert.toLongArray(ids);
        List<JacTestProject> jacTestProjects=new ArrayList<JacTestProject>();
        for (Long s:myids) {
            jacTestProjects.add(jacTestProjectMapper.selectJacTestProjectById(s));
        }
        List<String> list = new ArrayList<String>();
        Long itemcode=jacTestProjects.get(0).getItemcode();
        JacTestOutline jacTestOutline=jacTestOutlineMapper.selectJacTestOutlineById(itemcode);
            if("finish".equals(jacTestOutline.getStatus())){
                result="计划"+jacTestOutline.getItemcode()+"已完成不可重置！";
                list.add(result);
            }
        if (list.size() > 0) {
            result = Joiner.on(';').join(list);
            return result;
        }
        for (JacTestProject jacTestProject1 : jacTestProjects) {
                     jacTestProject1.setExperimentstatus("create");
                     jacTestProject1.setExperimentresult("new");
                     jacTestProject1.setPstage("beforeaudit");
                     jacTestProjectMapper.updateJacTestProject(jacTestProject1);
        }
        return result;
    }

    
    /**
     * 每当对单个或批量的project进行增删改查后会导致整体的project的状态变更
     * 此方法用来检测整体project的变更，以及变更后更改大纲的状态
     * 传递参数： itemcode、result
     * 返回结果： result
     */


    public  String getResultAndCheckOutlineStatus(String result, Long itemcode) {
        List<JacTestProject> jtp = jacTestProjectMapper.selectJacTestProjectByItemcode(itemcode);
        jacTestOutlineMapper.setFinishTimeNUll(itemcode);
        JacTestOutline jto = jacTestOutlineMapper.selectJacTestOutlineByIdNew(itemcode);
        int createcount=0;
        for (JacTestProject j : jtp) {
            if (("create".equals(j.getExperimentstatus()))) {
                createcount++;
            }else if (("ongoing".equals(j.getExperimentstatus()))) {
                jto.setStatus("underway");
                jacTestOutlineMapper.updateJacTestOutline(jto);
                return result;
            }
        }
        if(jtp.size()==createcount) {
            jto.setStatus("notbegin");
            jacTestOutlineMapper.updateJacTestOutline(jto);
            return result;
        }
        if(createcount<jtp.size()&&createcount>0){
            jto.setStatus("underway");
            jacTestOutlineMapper.updateJacTestOutline(jto);
            return result;
        }
        for (JacTestProject j:jtp) {
            if("notstandard".equals(j.getExperimentstatus()))
            {
                jto.setStatus("unqualified");
                jacTestOutlineMapper.updateJacTestOutline(jto);
                return result;
            }
        }
        jto.setStatus("underway");
        jacTestOutlineMapper.updateJacTestOutline(jto);
        return result;
    }
}